import 'dart:math';

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // home: RoateAnimationExample3()
      // home: RotationAnimationExample(),
      // home: MultiStageRotationExample(),
      // home: LoadingSpinner(),
      home: InteractiveRotationCard(),
    );
  }
}

class RoateAnimationExample3 extends StatefulWidget {
  @override
  _RoateAnimationExample3State createState() => _RoateAnimationExample3State();
}

class _RoateAnimationExample3State extends State<RoateAnimationExample3>
    with SingleTickerProviderStateMixin {
  late AnimationController _animationController;
  late Animation<double> _rotaingAnimation;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    _animationController = AnimationController(
      vsync: this,
      duration: Duration(seconds: 2),
    );

    _rotaingAnimation = Tween<double>(
      begin: 0,
      end: pi,
    ).animate(_animationController);
  }

  @override
  void dispose() {
    super.dispose();
    _animationController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('旋转动画示例')),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            AnimatedBuilder(
              animation: _rotaingAnimation,
              builder: (BuildContext context, Widget? child) {
                return Transform.rotate(
                  // 2派  180度 就派
                  angle: _rotaingAnimation.value,
                  child: Container(
                    width: 250,
                    height: 110,
                    color: Colors.red,
                    child: Image.asset('asset/images/s03.png'),
                  ),
                );
              },
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.change_circle_outlined),
        onPressed: () {
          if (_animationController.status == AnimationStatus.completed) {
            _animationController.reverse();
          } else {
            _animationController.forward();
          }
        },
      ),
    );
  }
}

class RotationAnimationExample extends StatefulWidget {
  @override
  _RotationAnimationExampleState createState() =>
      _RotationAnimationExampleState();
}

class _RotationAnimationExampleState extends State<RotationAnimationExample>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _animation;

  @override
  void initState() {
    super.initState();

    // 初始化动画控制器
    _controller = AnimationController(
      duration: const Duration(seconds: 2),
      vsync: this,
    );

    // 使用 CurvedAnimation 添加缓动效果
    final CurvedAnimation curvedAnimation = CurvedAnimation(
      parent: _controller,
      curve: Curves.easeInOut,
    );

    _animation = Tween<double>(
      begin: 0,
      end: 2 * 3.14159,
    ).animate(curvedAnimation);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('旋转动画示例')),
      body: Center(
        child: AnimatedBuilder(
          animation: _animation,
          builder: (context, child) {
            return Transform.rotate(
              angle: _animation.value,
              child: Container(
                width: 100,
                height: 100,
                color: Colors.blue,
                child: Icon(Icons.star, color: Colors.white, size: 50),
              ),
            );
          },
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          if (_controller.isCompleted) {
            _controller.reverse();
          } else {
            _controller.forward();
          }
        },
        child: Icon(Icons.play_arrow),
      ),
    );
  }
}

class MultiStageRotationExample extends StatefulWidget {
  @override
  _MultiStageRotationExampleState createState() =>
      _MultiStageRotationExampleState();
}

class _MultiStageRotationExampleState extends State<MultiStageRotationExample>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _rotationAnimation;

  @override
  void initState() {
    super.initState();

    _controller = AnimationController(
      duration: const Duration(seconds: 4),
      vsync: this,
    );

    // 创建分段动画
    _rotationAnimation = TweenSequence<double>([
      TweenSequenceItem(
        tween: Tween<double>(begin: 0, end: 0.5 * 3.14159), // 0° 到 90°
        weight: 1,
      ),
      TweenSequenceItem(
        tween: Tween<double>(
          begin: 0.5 * 3.14159,
          end: 1.5 * 3.14159,
        ), // 90° 到 270°
        weight: 2,
      ),
      TweenSequenceItem(
        tween: Tween<double>(
          begin: 1.5 * 3.14159,
          end: 2 * 3.14159,
        ), // 270° 到 360°
        weight: 1,
      ),
    ]).animate(_controller);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('多阶段旋转')),
      body: Center(
        child: AnimatedBuilder(
          animation: _rotationAnimation,
          builder: (context, child) {
            return Transform.rotate(
              angle: _rotationAnimation.value,
              child: Container(
                width: 120,
                height: 120,
                decoration: BoxDecoration(
                  gradient: LinearGradient(
                    colors: [Colors.blue, Colors.purple],
                    begin: Alignment.topLeft,
                    end: Alignment.bottomRight,
                  ),
                  borderRadius: BorderRadius.circular(20),
                ),
                child: Icon(Icons.rocket_launch, color: Colors.white, size: 60),
              ),
            );
          },
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          if (_controller.status == AnimationStatus.completed) {
            _controller.reverse();
          } else {
            _controller.forward();
          }
        },
        child: Icon(Icons.swap_horiz),
      ),
    );
  }
}

class LoadingSpinner extends StatefulWidget {
  final double size;
  final Color color;

  const LoadingSpinner({Key? key, this.size = 40, this.color = Colors.blue})
    : super(key: key);

  @override
  _LoadingSpinnerState createState() => _LoadingSpinnerState();
}

class _LoadingSpinnerState extends State<LoadingSpinner>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(milliseconds: 800),
      vsync: this,
    )..repeat();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return RotationTransition(
      turns: _controller,
      child: Container(
        width: widget.size,
        height: widget.size,
        decoration: BoxDecoration(color: widget.color, shape: BoxShape.circle),
        child: Icon(
          Icons.autorenew,
          color: Colors.white,
          size: widget.size * 0.6,
        ),
      ),
    );
  }
}

class InteractiveRotationCard extends StatefulWidget {
  @override
  _InteractiveRotationCardState createState() =>
      _InteractiveRotationCardState();
}

class _InteractiveRotationCardState extends State<InteractiveRotationCard>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _rotationAnimation;
  bool _isRotated = false;

  @override
  void initState() {
    super.initState();

    _controller = AnimationController(
      duration: const Duration(milliseconds: 500),
      vsync: this,
    );

    _rotationAnimation = Tween<double>(
      begin: 0,
      end: 0.5 * 3.14159, // 180度
    ).animate(CurvedAnimation(parent: _controller, curve: Curves.easeInOut));
  }

  void _toggleRotation() {
    setState(() {
      _isRotated = !_isRotated;
    });

    if (_isRotated) {
      _controller.forward();
    } else {
      _controller.reverse();
    }
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: GestureDetector(
          onTap: _toggleRotation,
          child: AnimatedBuilder(
            animation: _rotationAnimation,
            builder: (context, child) {
              return Transform.rotate(
                angle: _rotationAnimation.value,
                child: Container(
                  width: 200,
                  height: 200,
                  decoration: BoxDecoration(
                    gradient: LinearGradient(
                      colors: _isRotated
                          ? [Colors.purple, Colors.pink]
                          : [Colors.blue, Colors.green],
                      begin: Alignment.topLeft,
                      end: Alignment.bottomRight,
                    ),
                    borderRadius: BorderRadius.circular(20),
                    boxShadow: [
                      BoxShadow(
                        color: Colors.black26,
                        blurRadius: 10,
                        offset: Offset(0, 5),
                      ),
                    ],
                  ),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Icon(
                        _isRotated
                            ? Icons.flip_camera_android
                            : Icons.flip_camera_ios,
                        color: Colors.white,
                        size: 60,
                      ),
                      SizedBox(height: 10),
                      Text(
                        _isRotated ? '点击翻转回来' : '点击翻转',
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 16,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ],
                  ),
                ),
              );
            },
          ),
        ),
      ),
    );
  }
}
